// 菜单表转树结构
function menusTree(menus, pid) {
  const list = []
  menus.forEach((menu) => {
    if (menu.parentId === pid) {
      const children = menusTree(menus, menu.id)
      if (children.length) {
        menu.children = children
      }
      const routerPath = menu.path
      const routerName = routerPath.slice(routerPath.lastIndexOf('/') + 1, routerPath.length)
      const obj = {
        hidden: menu.hidden == '0' ? false : true,
        name: routerName,
        type: menu.menuType,
        path: routerPath,
        component: menu.component,
        meta: { title: menu.menuName, icon: menu.icon, cache: menu.isCache ? false : true, link: menu.linkUrl },
        children: menu.children ?? [],
      }
      if (menu.menuType != 0) delete obj.children
      list.push(obj)
    }
  })
  return list
}

// 接口返回的数据结构
function resDesign({ code = 200, data = null, msg = '请求成功' } = {}) {
  if (data) {
    return {
      code,
      data,
      msg,
    }
  } else {
    return {
      code,
      msg,
    }
  }
}

module.exports = { menusTree, resDesign }
